import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/qc/PQCFunctionController.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';

class PQCFunctionPage extends StatelessWidget {
  final controller = Get.put(PQCFunctionController());
  final state = Get.find<PQCFunctionController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(USER),
      ),
      body: SingleChildScrollView(
        child: GetBuilder<PQCFunctionController>(
          builder: (controller) => Container(
            child: Column(
              children: [
                SubTitleBar("PQC功能", "提交", () {
                  controller.submit();
                }),
                TitleEditText("生产批号：", "请扫描生产批号", (value) {
                    state.lotID = value;
                    controller.getLotInfo();
                }),
                SizedBox(
                  height: 1,
                ),
                TitleEditText("设备编号：", "请扫描设备编号", (value) {
                    state.eqpID = value;
                }),
                SizedBox(
                  height: 1,
                ),
                GetBuilder<PQCFunctionController>(
                    builder: (controller) => Container(
                      child: Column(
                        children: [
                          TextView("当前工序：", state.lotEntity?.sTEPDESC.toString() ?? ''),
                          SizedBox(
                            height: 1,
                          ),
                          TextView("当前数量：", state.lotEntity?.mAINQTY.toString() ?? '0'),
                          SizedBox(
                            height: 1,
                          ),
                        ],
                      ),
                    ),
                  id: state.lotEntityID,
                ),
                TitleEditText("抽检数量：", "请输入抽检数量", (value) {
                  state.qtyValue = value;
                },type: TextInputType.number,isScan: false,),
                Container(
                  height: 48,
                  padding: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  width: double.infinity,
                  child: Stack(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "录入抽检信息",
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                      )
                    ],
                  ),
                ),
                // TitleEditText("抽检数量：", "请输入抽检数量", (value) {}),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<PQCFunctionController>(
                  builder: (controller) => Align(
                    alignment: Alignment.center,
                    child: Row(
                      children: [
                        SizedBox(
                          width: 12,
                        ),
                        Text(
                          "缺陷代码",
                          style: TextStyle(
                            color: text_black.toColor(),
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Expanded(
                            flex: 1,
                            child: Container(
                              padding: EdgeInsets.only(left: 4),
                              height: 36,
                              decoration: BoxDecoration(
                                  border: new Border.all(
                                      width: 1.0, color: theme_blue.toColor()),
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(4))),
                              child: DropdownButtonHideUnderline(
                                child: DropdownButton(
                                  value: state.selectDefectValue,
                                  items: getList(),
                                  onChanged: (value) {
                                    controller.updateSelectDefectValue(
                                        value as LotSCRAPCODELIST);
                                  },
                                  hint: Text(
                                    "请选择缺陷代码",
                                    style: TextStyle(
                                      fontSize: 14,
                                    ),
                                  ),
                                ),
                              ),
                            )),
                        SizedBox(
                          width: 8,
                        ),
                        Container(
                          height: 36,
                          margin: EdgeInsets.only(right: 8),
                          child: ElevatedButton(
                            onPressed: () {
                              controller.addDefectData();
                            },
                            child: Text("保存"),
                          ),
                        ),
                        SizedBox(
                          width: 4,
                        ),
                      ],
                    ),
                  ),
                  id: state.defectCodeLogicId,
                ),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<PQCFunctionController>(
                    builder: (controller) => Table(
                          defaultVerticalAlignment:
                              TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(2.0),
                            2: FlexColumnWidth(1.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getDefectList(context),
                        ),
                    id: state.defectTableLogicId),
                GetBuilder<PQCFunctionController>(
                    builder: (controller) =>
                        TextView("缺陷数量", state.defectCount.toString()),
                    id: state.defectCountLogicId),
                Row(
                  children: [
                    Expanded(
                      child:  Container(
                        height: 36,
                        margin: EdgeInsets.only(left: 8,right: 8),
                        child: ElevatedButton(
                          onPressed: () {
                              state.PQCRESULT = 'OK';
                          },
                          child: Text("OK"),
                        ),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child:     Container(
                        height: 36,
                        margin: EdgeInsets.only(left: 8,right: 8),
                        child: ElevatedButton(
                          onPressed: () {
                            state.PQCRESULT = 'NG';
                          },
                          child: Text("NG"),
                           style: ButtonStyle(backgroundColor: MaterialStateColor.resolveWith((states) => Colors.grey)),
                        ),
                      ),
                      flex: 1,
                    ),
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  List<TableRow> getDefectList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '缺陷代码',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '缺陷描述',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.defectList.length; i++) {
      LotSCRAPCODELIST element = state.defectList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.cODE ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.dESCRIPTION ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "",
              (value) {
                if (value.isEmpty) {
                  value = "0";
                }
                element.DEFECTQTY = value;
                controller.updateDefectCount();
              },
              type: TextInputType.number,
              isScan: false,
              isShowIcon: false,
              controller : TextEditingController(text: element.DEFECTQTY),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }

  List<DropdownMenuItem<LotSCRAPCODELIST>> getList() {
    List<DropdownMenuItem<LotSCRAPCODELIST>> mList = [];
    state.scrapCodeList.forEach((element) {
      mList.add(DropdownMenuItem(
        child: Text(element.dESCRIPTION ?? ''),
        value: element,
      ));
    });
    return mList;
  }
}
